package com.zx.crontask.config;

import cn.hutool.core.thread.ExecutorBuilder;
import lombok.Data;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @ClassName ExecutorConfig
 * @Description TODO
 * @Author Zx
 * @Date 2022年09月29日 21:18
 * @Version 1.0
 **/
@Data
@EnableAsync
@Configuration
public class ExecutorConfig implements AsyncConfigurer {


    /**
     * 这里我选择的是线程池单例
     *
     * 这里配置的糊涂工具类的线程池，创建以下参数自定义或者配置到配置文件进行获取
     * CorePoolSize 核心线程数
     * MaxPoolSize  最大线程数
     * @return ExecutorService
     */
    @Bean(name = "pool")
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public ExecutorService createExecutor(){
        return ExecutorBuilder.create()
                .setCorePoolSize(6)
                .setMaxPoolSize(7)
                .setWorkQueue(new LinkedBlockingQueue<>(50))
                .build();
    }

    @Override
    public Executor getAsyncExecutor() {
        return createExecutor();
    }

}
